<?php session_start();
include "statics.php";
$get_id=$_SESSION["member_id"];
$sqlquery13=rsSelect("area_of_expertise_id","area_of_expertise",array("area_of_expertise_text=",$_POST['papertype']));  // query13 tries to get thev area of expertise id in the database by comparing areaofexpertise text with the text choosen by the user and confirm the id then store this id in a new variable then it is able to insert  it in the conference table.
if(is_string($sqlquery13))
{
    echo $sqlquery13 . "ERROR CODE: 0" . __LINE__;
    exit();
}
if(mysql_num_rows($sqlquery13) == 0)
{
    echo "ERROR CODE: 0" . __LINE__;
    exit();
}
$row7=mysql_fetch_assoc($sqlquery13); // $row7 stores the id in an array  and then i pinpoint the element that i need (in this case areaofexpertise id that i got from the last query)
$area_of_expertise_id1=$row7['area_of_expertise_id'];  // i store the element of areaofexpertise id in a new variable so i can use  it in the following insert query
$regexp="/^20(1[0-9]|2[0-9]|3[0-8])(-|\/)\d{2}(-|\/)\d{2}$/";
$curdate=date("Y-m-d");
if(preg_match($regexp,$_POST['review_end']) == 0)
{
    echo "ERROR CODE: 0" . __LINE__;
    exit();
}
$review_end_date=date("Y-m-d",strtotime($_POST['review_end']));
$rsPlace=rsSelect("country_id","country",array("country_id=",$_POST['place']));
if(is_string($rsPlace))
{
    echo $rsPlace . "ERROR CODE: 0" . __LINE__;
    exit();
}
if(mysql_num_rows($rsPlace) == 0)
{
    echo "ERROR CODE: 0" . __LINE__;
    exit();
}
if($review_end_date <= $curdate)
{
    echo "ERROR CODE: 0" . __LINE__;
    exit();
}
if(strlen($_POST['textarea']) > 2000 || strlen($_POST['longname']) > 500 || strlen($_POST['shortname']) > 50 || strlen($_POST['call_papers']) > 200)
{
    echo "ERROR CODE: 0" . __LINE__;
    exit();
}
if (isset($_POST['rebuttaldate']) && $_POST['rebuttaldate'] != NULL) {
    $date = $_POST['rebuttaldate'];
$sqlquery=iInsert("conference",array('rebuttal_date'=>$date,'area_of_expertise'=>$area_of_expertise_id1,'long_name'=>$_POST['longname'],'short_name'=>$_POST['shortname'],"rev_startDate"=>"0000-00-00","rev_endDate"=>$_POST['review_end'],'description'=>$_POST['textarea'],"call_for_papers_text"=>$_POST['call_papers'],"creator_id"=>$get_id,"place"=>$_POST['place'],"conference_date"=>"E__NOW()"));  //i inserted all the need data in the conference table while making sure that it was the same data the user had chosen.used from statics.php
if(is_string($sqlquery))
{
    echo $sqlquery . "ERROR CODE: 0" . __LINE__;
    exit();
}
} else {
$sqlquery=iInsert("conference",array('area_of_expertise'=>$area_of_expertise_id1,'long_name'=>$_POST['longname'],'short_name'=>$_POST['shortname'],"rev_startDate"=>"0000-00-00","rev_endDate"=>$_POST['review_end'],'description'=>$_POST['textarea'],"call_for_papers_text"=>$_POST['call_papers'],"creator_id"=>$get_id,"place"=>$_POST['place'],"conference_date"=>"E__NOW()"));  //i inserted all the need data in the conference table while making sure that it was the same data the user had chosen.used from statics.php
if(is_string($sqlquery))
{
    echo $sqlquery . "ERROR CODE: 0" . __LINE__;
    exit();
}
}
$confid=mysql_insert_id(); //this query helps me to pinpoint the id of the newly created conference  and store it in the $confid after getting it  as an element of the $row array
//this query tries to get the id of the old created conference id from the database and insert this new id as an element in $row8
if((int)$_POST['conference_id'] != 0)
{
    $rsConference=rsSelect("COUNT(conference_id)","conference",array("conference_id=",$_POST['conference_id']));
    if(is_string($rsConference))
    {
        echo $rsConference . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $row=mysql_fetch_assoc($rsConference);
    if((int)$row['COUNT(conference_id)'] == 0)
    {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $sqlquery14=iInsert("linked_conferences",array('conference_id1'=>$confid,'conference_id2'=>$_POST['conference_id'])); //this query stores the id of the newly created conference  and stores the id of the old chosen conference in the linked conference table so later on we can know each conference and the linked conference  to it.
    if(is_string($sqlquery14))
    {
        echo $sqlquery14 . "ERROR CODE: 0" . __LINE__;
        exit();
    }
}
if($_POST['comm3'] == 'true')  // this checks that the checkbox of committee 3 is checked and return true for it
{
    $sqlquery3=iInsert("conference_committee",array('conference_id'=>$confid,'committee_id'=>3));  // this query will insert in the table of the conference_committee  the id  of the created conference along with the id of the chosen committee with its right id
}
if($_POST['comm2'] == 'true')  // this checks that the checkbox of committee 2 is checked and return true for it
{

    $sqlquery10=iInsert("conference_committee",array('conference_id'=>$confid,'committee_id'=>2)); //  this query will insert in the table of the conference_committee  the id  of the created conference along with the id of the chosen committee with its right id
}
if($_POST['comm1'] == 'true') // this checks that the checkbox of committee 1 is checked and return true for it
{

    $sqlquery12=iInsert("conference_committee",array('conference_id'=>$confid,'committee_id'=>1));  //  this query will insert in the table of the conference_committee  the id  of the created conference along with the id of the chosen committee with its right id
}
if($_POST['custom_com'] == "true")  // if the user entered a custom committe insert that committe into committee table then fetch its id and add an entry in conference committee with the conference id and the newly acquired custom committee id
{
    $new_committee=$_POST['custom_com_text'];
    $query=iInsert("committees",array("committee_name"=>$new_committee));
    if(is_string($query))
    {
        echo $query . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    else
    {
        $last_id=mysql_insert_id();
        $query=iInsert("conference_committee",array("conference_id"=>$confid,"committee_id"=>$last_id));
        if(is_string($query))
        {
            echo $query . "ERROR CODE: 0" . __LINE__;
            exit();
        }
    }
}
if($_POST["spapersub"] != NULL) // this query checks if the checkbox of the shortpaper is selected and returns true for it then add it to conference category
{
    if(preg_match($regexp,$_POST['spapersub']) == 0)
    {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $short_paper_date=date("Y-m-d",strtotime($_POST['spapersub']));
    
    $iCategoryId=iCategoryId("Short Paper");
    if(is_string($iCategoryId))
    {
        echo $iCategoryId . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $sqlquery5=iInsert("conference_category",array('conference_id'=>$confid,'category_id'=>$iCategoryId,"submission_date"=>$_POST['spapersub'])); // insert in the table of conference_category the id of the created conference and the right id of the category that the user had chosen
    if(is_string($sqlquery5))
    {
        echo $sqlquery5 . "ERROR CODE: 0" . __LINE__;
        exit();
    }
}
if($_POST['lpapersub'] != NULL)// this query checks if the checkbox of the long paper is selected and returns true for it then add it to conference category
{
    if(preg_match($regexp,$_POST['lpapersub']) == 0)
    {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $long_paper_date=date("Y-m-d",strtotime($_POST['lpapersub']));
   
    $iCategoryId=iCategoryId("Long Paper");
    if(is_string($iCategoryId))
    {
        echo $iCategoryId . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $sqlquery7=iInsert("conference_category",array('conference_id'=>$confid,'category_id'=>$iCategoryId,"submission_date"=>$_POST['lpapersub']));  // insert in the table of conference_category the id of the created conference and the right id of the category that the user had chosen
    if(is_string($sqlquery7))
    {
        echo $sqlquery7 . "ERROR CODE: 0" . __LINE__;
        exit();
    }
}
if($_POST['imagesub'] != NULL) // this query checks if the checkbox of the image is selected and returns true for it then add it to conference category
{
    if(preg_match($regexp,$_POST['imagesub']) == 0)
    {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $image_date=date("Y-m-d",strtotime($_POST['imagesub']));
   
    $iCategoryId=iCategoryId("Image");
    if(is_string($iCategoryId))
    {
        echo $iCategoryId . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $sqlquery16=iInsert("conference_category",array("conference_id"=>$confid,"category_id"=>$iCategoryId,"submission_date"=>$_POST['imagesub']));
    if(is_string($sqlquery16))
    {
        echo $sqlquery16 . "ERROR CODE: 0" . __LINE__;
        exit();
    }
}
if($_POST['videosub'] != NULL) // this query checks if the checkbox of the video is selected and returns true for it then add it to conference category
{
    if(preg_match($regexp,$_POST['videosub']) == 0)
    {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $video_date=date("Y-m-d",strtotime($_POST['videosub']));
    
    $iCategoryId=iCategoryId("Video");
    if(is_string($iCategoryId))
    {
        echo $iCategoryId . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $sqlquery16=iInsert("conference_category",array("conference_id"=>$confid,"category_id"=>$iCategoryId,"submission_date"=>$_POST['videosub']));
    if(is_string($sqlquery16))
    {
        echo $sqlquery16 . "ERROR CODE: 0" . __LINE__;
        exit();
    }
}
if($_POST['postersub'] != NULL) // this query checks if the checkbox of the poster is selected and returns true for it then add it to conference category
{
    if(preg_match($regexp,$_POST['postersub']) == 0)
    {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $poster_date=date("Y-m-d",strtotime($_POST['postersub']));
    
    $iCategoryId=iCategoryId("Poster");
    if(is_string($iCategoryId))
    {
        echo $iCategoryId . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $sqlquery16=iInsert("conference_category",array("conference_id"=>$confid,"category_id"=>$iCategoryId,"submission_date"=>$_POST['postersub']));
    if(is_string($sqlquery16))
    {
        echo $sqlquery16 . "ERROR CODE: 0" . __LINE__;
        exit();
    }
}
$iRoleId=iRoleId("organizer");
if(is_string($iRoleId))
{
    echo $iRoleId . "ERROR CODE: 0" . __LINE__;
    exit();
}
$iAddRole=iInsert("member_privileges",array("member_id"=>$_SESSION['member_id'],"conference_id"=>$confid,"privileges_id"=>$iRoleId));
if(is_string($iAddRole))
{
    echo $iAddRole . "ERROR CODE: 0" . __LINE__;
    exit();
}
echo $confid;
?>